package net.wofly.cashier.dishes.service.impl;

import net.wofly.cashier.dishes.domain.DailyCommodityNum;
import net.wofly.cashier.dishes.service.IDailyCommodityNumService;
import net.wofly.cashier.dishes.service.IDishesService;
import net.wofly.common.util.CommonStatus;
import net.wofly.common.util.DateUtil;
import net.wofly.common.util.Identities;
import net.wofly.framework.util.ResultCode;
import net.wofly.framework.util.ReturnBean;
import net.wofly.sale.commodity.domain.Commodity;
import net.wofly.sale.commodity.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * Created by LiuJianbo on 2017/10/17.
 */
@Service("iDishesService")
@Transactional
public class DishesServiceImpl implements IDishesService {

    @Autowired
    ICommodityService iCommodityService;

    @Autowired
    IDailyCommodityNumService iDailyCommodityNumService;

    @Override
    public ReturnBean<List<Commodity>> getAllDishes(Integer status, String commodityTypeID) {

        ReturnBean<List<Commodity>> commodityList = iCommodityService.getCommodityByType(commodityTypeID, status);

        return commodityList;
    }

    /**
     * 获取所有菜品，同时返回菜品可卖数量
     * <ul>
     * <li>commodityTypeID=-1时，取所有类型的菜品</li>
     * <li>只取有效状态的菜品</li>
     * <li></li>
     * </ul>
     *
     * @param commodityTypeID
     */
    @Override
    public ReturnBean<List<Commodity>> getAllDishesByNum(String commodityTypeID) {
        List<Commodity> commodityList = null;
        if (commodityTypeID.equals("-1")) {//有效状态的所有菜品
            commodityList = iCommodityService.findByStatus(CommonStatus.有效.getValue());
        } else {
            commodityList = iCommodityService.findByCommodityTypeIDAndStatus(commodityTypeID, CommonStatus.有效.getValue());
        }
        if (!CollectionUtils.isEmpty(commodityList)) {
            Long time = DateUtil.processStartTime(new Date()).getTime();
            for (Commodity commodity : commodityList) {
                DailyCommodityNum dailyCommodityNum = iDailyCommodityNumService.findByCommodityIDAndSetDate(
                        commodity.getCommodityID(), time);
                if (dailyCommodityNum != null) {
                    commodity.setResidueNum(dailyCommodityNum.getResidueNum());
                } else {
                    commodity.setResidueNum(-1);
                }
            }
        }

        return new ReturnBean<List<Commodity>>(ResultCode.SUCCESS, "获取成功", commodityList);
    }

    /**
     * 获取菜品指定日的销售数量  获取的是可销售数量
     *
     * @param commodityID
     * @param currDate
     */
    @Override
    public ReturnBean<DailyCommodityNum> getValidDishesNum(String commodityID, Long currDate) {
        // 获取的是可销售数量
        Commodity commodity = iCommodityService.findOne(commodityID);
        if (commodity == null) {
            return new ReturnBean<DailyCommodityNum>(ResultCode.ERROR_SERVICE, "菜品ID不正确，未找到对应菜品");
        }
        Long setDate = DateUtil.processStartTime(new Date(currDate)).getTime();
        DailyCommodityNum commodityNum = iDailyCommodityNumService.findByCommodityIDAndSetDate(commodityID, setDate);
        if (commodityNum == null) {
            commodityNum = new DailyCommodityNum();
            commodityNum.setCommodityID(commodityID);
            commodityNum.setResidueNum(-1);//
        }
        return new ReturnBean<DailyCommodityNum>(ResultCode.SUCCESS, "获取成功", commodityNum);
    }

    /**
     * 设置菜品日最大销售量
     *
     * @param dailyCommodityNum
     */
    @Override
    public ReturnBean<DailyCommodityNum> setDailyDishesNum(DailyCommodityNum dailyCommodityNum) {
        //已经设置过，如果没有开卖，则允许重新设置，已经开卖则不允许重新设置，点菜时先校验可卖数量; 如果没有设置，则也可以进行点菜，数量不做控制

        Long setDate = dailyCommodityNum.getSetDate();
        if (null == setDate) {
            setDate = System.currentTimeMillis();
        }

        Long startTime = DateUtil.processStartTime(new Date(setDate)).getTime();
        dailyCommodityNum.setSetDate(startTime);

        DailyCommodityNum dailyCommodityNumDb = iDailyCommodityNumService.findByCommodityIDAndSetDate(
                dailyCommodityNum.getCommodityID(), startTime);

        if (dailyCommodityNumDb == null) {
            dailyCommodityNum.setDailyCommodityNumID(Identities.uuid2());
        } else {
            if (dailyCommodityNumDb.getNum().intValue() != dailyCommodityNumDb.getResidueNum()) {
                return new ReturnBean<DailyCommodityNum>(ResultCode.ERROR_SERVICE, "此菜品今日已经开卖，不能重新设置可卖数量");
            }
        }
        dailyCommodityNum.setResidueNum(dailyCommodityNum.getNum());
        dailyCommodityNum.setModifyDate(System.currentTimeMillis());
        return new ReturnBean<DailyCommodityNum>(ResultCode.SUCCESS, "设置成功",
                iDailyCommodityNumService.save(dailyCommodityNum));
    }
}
